package com.huluobo.lc.libcommon;

import android.content.res.TypedArray;
import android.graphics.Outline;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;

import java.util.jar.Attributes;

/**
 * @author Lc
 * @description:
 * @date :2020/4/3 9:52
 */
public class ViewHelper {
    public static final int RADIUS_ALL = 0;
    public static final int RADIUS_LEFT = 1;
    public static final int RADIUS_TOP = 2;
    public static final int RADIUS_RIGHT = 3;
    public static final int RADIUS_BOTTOM = 4;

    public static void setViewOutline(View view, AttributeSet attributes, int defStyleAttr,
                                      int defStyleRes) {
        TypedArray typedArray = view.getContext().obtainStyledAttributes(attributes,
                R.styleable.viewOutLineStrategy, defStyleAttr, defStyleRes);
        int radius =
                typedArray.getDimensionPixelOffset(R.styleable.viewOutLineStrategy_clip_radius, 0);
        int radiusSide = typedArray.getIndex(R.styleable.viewOutLineStrategy_clip_side);
        typedArray.recycle();

        setViewOutline(view, radius, radiusSide);
    }

    public static void setViewOutline(View view, final int radius, final int radiusSide) {
        if (radius <= 0) {
            return;
        }

        view.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                int width = view.getWidth();
                int height = view.getHeight();

                if (width <= 0 || height <= 0) {
                    return;
                }

                if (radiusSide != RADIUS_ALL) {
                    int lef = 0, right = width, top = 0, bottom = height;
                    if (radiusSide < RADIUS_LEFT) {
                        right += radius;
                    } else if (radiusSide == RADIUS_TOP) {
                        bottom += radius;
                    } else if (radiusSide == RADIUS_RIGHT) {
                        lef -= radius;
                    } else if (radiusSide == RADIUS_BOTTOM) {
                        top -= radius;
                    }

                    outline.setRoundRect(lef, top, right, bottom, radius);
                } else {
                    if (radius > 0) {
                        outline.setRoundRect(0, 0, width, height, radius);
                    } else {
                        outline.setRect(0, 0, width, height);
                    }
                }
            }
        });
    }
}
